package com.example.service;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.ObjectUtil;
import com.example.common.enums.RoleEnum;
import com.example.entity.Account;
import com.example.entity.Charge;
import com.example.entity.Dorm;
import com.example.entity.Student;
import com.example.exception.CustomException;
import com.example.mapper.ChargeMapper;
import com.example.mapper.DormMapper;
import com.example.utils.TokenUtils;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.List;

/**
 * 电费缴费信息表业务处理
 **/
@Service
public class ChargeService {

    @Resource
    private ChargeMapper chargeMapper;

    @Resource
    private StudentService studentService;

    /**
     * 新增
     */
    @Transactional(rollbackFor = Exception.class)
    public void add(Charge charge) {
        Charge dbCharge = chargeMapper
                .selectByDorm(charge.getDormId());
        if(ObjectUtil.isNotNull(dbCharge) && (charge.getTime().equals(charge.getTime()))){
            // 判断是不是今年本月
            throw new CustomException("500","当前宿舍该月电费已发放");
        }
        checkPowerAndPrice(charge);
        charge.setAmount(BigDecimal.valueOf(charge.getPower()).multiply(charge.getPrice()));
        charge.setStatus("未缴费");
        chargeMapper.insert(charge);
    }


    /**
     * 删除
     */
    @Transactional(rollbackFor = Exception.class)
    public void deleteById(Integer id) {
        chargeMapper.deleteById(id);
    }

    /**
     * 批量删除
     */
    @Transactional(rollbackFor = Exception.class)
    public void deleteBatch(List<Integer> ids) {
        for (Integer id : ids) {
            chargeMapper.deleteById(id);
        }
    }

    /**
     * 修改
     */
    @Transactional(rollbackFor = Exception.class)
    public void updateById(Charge charge) {
        checkPowerAndPrice(charge);
        charge.setAmount(BigDecimal.valueOf(charge.getPower()).multiply(charge.getPrice()));
        chargeMapper.updateById(charge);
    }

    private void checkPowerAndPrice(Charge charge) {
        if(charge.getPower() <= 0){
            throw new CustomException("500","用电度数不能为0");
        }
        if(charge.getPrice().compareTo(BigDecimal.ZERO) <= 0){
            throw new CustomException("500","缴费单价不能为0");
        }
    }

    /**
     * 根据ID查询
     */
    public Charge selectById(Integer id) {
        return chargeMapper.selectById(id);
    }

    /**
     * 查询所有
     */
    public List<Charge> selectAll(Charge charge) {
        return chargeMapper.selectAll(charge);
    }

    /**
     * 分页查询
     */
    public PageInfo<Charge> selectPage(Charge charge, Integer pageNum, Integer pageSize) {
        Account currentUser = TokenUtils.getCurrentUser();
        if(currentUser.getRole().equals(RoleEnum.STUDENT.name())){
            // 查询学生是否绑定宿舍
            Student student = studentService.selectById(currentUser.getId());
            if(ObjectUtil.isNull(student)){
                throw new CustomException("500","学生不存在");
            }
            if(student.getDormId() == null){
                throw new CustomException("505","请先关联宿舍再进行缴费");
            }
            charge.setDormId(student.getDormId());
        }
        PageHelper.startPage(pageNum, pageSize);
        List<Charge> list = chargeMapper.selectAll(charge);
        return PageInfo.of(list);
    }

    /**
     * 支付
     * @param charge
     */
    @Transactional(rollbackFor = Exception.class)
    public void payById(Charge charge) {
        Account currentUser = TokenUtils.getCurrentUser();
        if(currentUser.getRole().equals(RoleEnum.STUDENT.name())){
            // 查询学生是否绑定宿舍
            Student student = studentService.selectById(currentUser.getId());
            if(ObjectUtil.isNull(student)){
                throw new CustomException("500","学生不存在");
            }
            if(student.getDormId() == null){
                throw new CustomException("500","请联系管理员，先关联宿舍再进行缴费");
            }
        }
        charge.setStatus("已缴费");
        chargeMapper.updateById(charge);
    }
}